package com.hhd.pdf.components;


import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfDiv;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class SectionComponent extends AbstractPdfComponent {

    @Override
    public void parseComponent(Element element, GeneratorContext context) throws Exception {
        Font font = PdfUtils.createFont(11, PdfUtils.createColor(48, 111, 220));
        List<com.itextpdf.text.Element> elements = parseSimpleNode(element, context);
        Image image = PdfUtils.getImage("SectionBar.png");

        PdfPTable table = PdfUtils.createTable(new float[]{1, 50});
        PdfPCell cell = PdfUtils.createCell(image);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);

        PdfPCell pdfPCell = PdfUtils.getPdfPCell();
        for (com.itextpdf.text.Element element1 : elements) {
            if (element1 instanceof Phrase) {
                pdfPCell.addElement(PdfUtils.changeFont((Phrase) element1, font));
            } else {
                pdfPCell.addElement(element1);
            }
        }
        pdfPCell.setMinimumHeight(27);
        pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        table.addCell(pdfPCell);

        PdfDiv pdfDiv = PdfUtils.wrapDiv(table, 5, 10, 0, 10);
        context.getDocument().add(pdfDiv);
        context.getDocument().add(PdfUtils.createDivider(5, 15, 0, 15));
    }

    @Override
    public String getName() {
        return "section";
    }
}
